<?php
/*------------------------------------------------------ */
//-- 微信直播间
//-- @author iqgmy
/*------------------------------------------------------ */

namespace app\live\model;

use app\BaseModel;
use think\facade\Cache;
use app\weixin\model\WeiXinMpModel;

class LiveRoomModel extends BaseModel
{
    protected $table = 'live_room';
    public $pk = 'id';
    public $live_status = ['101' => '直播中', '102' => '未开始', '103' => '已结束', '104' => '禁播', '105' => '暂停中', '106' => '异常', '107' => '已过期'];


    public function getLastOne(){
        $res = $this->where('live_status', 101)
            ->where('start_time', '<', time())
            ->order('start_time DESC')
            ->find();
        return $res;
    }

    /**
     *获取列表
     */
    public function getReplayList($page,$limit = 10)
    {
        $mkey = 'live_room_replay_list_'.$page;
        //$list = Cache::get($mkey);
        //if (empty($list) == false) return $list;
        if ($page > 0){
            $page = $page - 1;
        }else{
            $page = 0;
        }
        $start = $page * $limit;
        $rows = $this->where('is_replay',1)->order('start_time DESC')->limit($start,$limit)->select()->toArray();
        if (empty($rows)) return [];
        foreach ($rows as $key=>$row){
            $row['end_time_data'] = date('Y-m-d H:i',$row['end_time']);
            $row['start_time_data'] = date('Y-m-d H:i',$row['start_time']);
            $row['goods'] = json_decode($row['goods'],true);
            $row['live_status_txt'] = $this->live_status[$row['live_status']];
            $row['replay_status'] = 0;
            if ($row['live_status'] == 103 && $row['is_replay'] == 1){
                if (empty($row['live_replay']) == false){
                    $row['replay_status'] = 1;
                }
            }
            unset($row['anchor_user_id'],$row['live_replay']);
            $rows[$key] = $row;
        }
        Cache::set($mkey,$rows,30);
        return $rows;
    }


    /**
     *获取房间数据
     */
    public function getReplayInfo($roomid)
    {
        $mkey = 'live_room_replay_info_'.$roomid;
        $roomInfo = Cache::get($mkey);
        if (empty($roomInfo) == false) return $roomInfo;
        $roomInfo = $this->where('roomid',$roomid)->find();
        if (empty($roomInfo)){
            return false;
        }
        $roomInfo = $roomInfo->toArray();
        $roomInfo['live_replay'] = json_decode($roomInfo['live_replay'],true);
        Cache::set($mkey,$roomInfo,30);
        return $roomInfo;
    }


    public function saveRoom($anchor, $data){
        $liveRoom = $this->where('live_status', 102)
            ->where('anchor_user_id', $anchor['user_id'])
            ->where('herald_time', '>', time())
            ->find();
        if (!$liveRoom) $liveRoom = new self();

        $data['anchor_user_id'] = $anchor['user_id'];
        $data['anchor_name'] = $anchor['nick_name'];
        $data['start_time'] = time();
        if (!isset($data['herald_time']) && empty($data['herald_time'])) $data['herald_time'] = time();

        if (!empty($data['thumb'])){
            $file_path = config('config._upload_').'live/'.date('Ym');
            $data['cover_img'] = copyFile($data['thumb'], $file_path);
        }

        return $liveRoom->save($data);

    }

    public function getLiveRoomByRoomid($roomid){
        return $this->where('roomid', $roomid)->find();
    }

}

?>